<?php
declare (strict_types = 1);

namespace app\command\mobile;

use api\mobile\Lechong;
use app\model\MobileOrder;
use app\model\MobileSupplier;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class QueryLechong extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('querylechong')
            ->setDescription('乐充订单查询');
    }

    protected function execute(Input $input, Output $output)
    {

       $supplier =  MobileSupplier::query()
            ->where('code','lechong')
            ->find();

       if (!$supplier){
           echo '没有乐充供应商';
           return;
       }

        MobileOrder::query()
//            ->where('order_id','22070114150273371_19')
            ->where('mobile_supplier_id',$supplier->id)
            ->where('refund_status',1)
            ->where('supplier_order_create_time','<',time()-3600)
            ->where('supplier_status',21) //需要人工待确认的
            ->select()->each(function (MobileOrder $mobileOrder)use($supplier){
                //处理人工待确认的订单

                $model  = new Lechong($supplier->toArray());
                $data =$model->query($mobileOrder->sub_order_id);

                var_dump($data);

                if ($data['recode'] == 'q106'){

                    $update = [
                        'supplier_status' => 99,
                        'remark' => '',
                    ];

                    MobileOrder::where('id',$mobileOrder->id)
                        ->where('supplier_status', 21)
                        ->update($update);
                    echo '成功处理一条';


                    return;
                }


                if (empty($data['state'])){
                    return;
                }

                if (in_array($data['state'],[4,-11])){
                    //一定失败

                    $update = [
                        'supplier_status' => 99,
                        'remark' => '',
                    ];

                    MobileOrder::where('id',$mobileOrder->id)
                        ->where('supplier_status', 21)
                        ->update($update);
                    echo '成功处理一条';
                    return;
                }



            });

    }
}
